<?php

/**
 * OrangeHRM is a comprehensive Human Resource Management (HRM) System that captures
 * all the essential functionalities required for any enterprise.
 * Copyright (C) 2006 OrangeHRM Inc., http://www.orangehrm.com
 *
 * OrangeHRM is free software; you can redistribute it and/or modify it under the terms of
 * the GNU General Public License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * OrangeHRM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program;
 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA
 */

if('ROOT_PATH'==ROOT_PATH){

}

require_once ROOT_PATH . '/lib/confs/sysConf.php';
require_once ROOT_PATH . '/lib/exception/ExceptionHandler.php';
require_once ROOT_PATH . '/lib/common/FormCreator.php';
require_once ROOT_PATH . '/lib/common/authorize.php';
require_once ROOT_PATH . '/lib/common/TemplateMerger.php';
require_once ROOT_PATH . '/lib/common/AjaxCalls.php';

require_once ROOT_PATH . '/lib/models/pa/paResults.php';



require_once ROOT_PATH . '/lib/extractor/pa/EXTRACTOR_PA_Results.php';


/**
 * Controller for skill matrix module
 */
class PAController {

	private $authorizeObj;

    /**
     * Constructor
     */
    public function __construct() {
        if (isset($_SESSION) && isset($_SESSION['fname']) ) {
			$this->authorizeObj = new authorize($_SESSION['empID'], $_SESSION['isAdmin']);
        }
    }

    /**
     * Handle incoming requests
     * @param String code Recruit code
     */
    public function handleRequest($code) {

		if (empty($code) ) {
			trigger_error("Invalid Action " . $_GET['action'], E_USER_NOTICE);
			return;
		}

		switch ($code) {

			case 'PA' :
				$paExtractor = new EXTRACTOR_PA_Results();
				if(isset($_GET['id']))
					$id = $_GET['id'];
				else
					$id = null;

				$userID = $_SESSION['empID'];
	            switch ($_GET['action']) {
	            	case 'Import':
	            		$conf = new Conf();
	            		if(isset($_SESSION['userName']) && in_array($_SESSION['userName'],$conf->paAccounts)){
	            			return $this->_viewImport();
	            		}else{
	            			$this->_personalResults($userID);
	            		}
	            		break;
	            	case 'SaveImport':
	            		$paResults = $paExtractor->parseImportPAResults();
	            		$this->_saveImport($paResults);
	            		break;
	            	case 'PersonalResults':
	            		$this->_personalResults($userID);
	            		break;
	            }
                break;

	    }
    }
    private function _viewImport(){
    	$records= PAResults::getAll();
		$path = "/templates/pa/import.php";
		$template = new TemplateMerger($records, $path);
		$template->display();
    }

	private function _saveImport($paResults){
	 	foreach($paResults as $paResult){
	 		$paResult->save();
	 	}
		$message ="Imported successfull";
    	$this->redirect($message);
    }

    private function _personalResults($empID){
    	$records= PAResults::getPAResultByEmpID($empID);
		$path = "/templates/pa/paDetails.php";
		$template = new TemplateMerger($records, $path);
		$template->display();
    }
	/**
	 * Redirect to given url or current page while displaying optional message
	 *
	 * @param String $message Message to display
	 * @param String $url URL
	 */
	public function redirect($message=null, $url = null) {

		if (isset($url)) {
			$mes = "";
			if (isset($message)) {
				$mes = "&message=";
			}
			$url=array($url.$mes);
			$id="";
		} else if (isset($message)) {
			preg_replace('/[&|?]+id=[A-Za-z0-9]*/', "", $_SERVER['HTTP_REFERER']);

			if (preg_match('/&/', $_SERVER['HTTP_REFERER']) > 0) {
				$message = "&message=".$message;
				$url = preg_split('/(&||\?)message=[A-Za-z0-9]*/', $_SERVER['HTTP_REFERER']);
			} else {
				$message = "?message=".$message;
			}

			if (isset($_REQUEST['id']) && !empty($_REQUEST['id']) && !is_array($_REQUEST['id'])) {
				$id = "&id=".$_REQUEST['id'];
			} else {
				$id="";
			}
		} else {
			if (isset($_REQUEST['id']) && !empty($_REQUEST['id']) && (preg_match('/&/', $_SERVER['HTTP_REFERER']) > 0)) {
				$id = "&id=".$_REQUEST['id'];
			} else if (preg_match('/&/', $_SERVER['HTTP_REFERER']) == 0){
				$id = "?id=".$_REQUEST['id'];
			} else {
				$id="";
			}
		}

		header("Location: ".$url[0].$message.$id);
	}

    /**
     * Show not authorized message
     */
    private function _notAuthorized() {
        trigger_error("Not Authorized!", E_USER_NOTICE);
    }
}
?>